﻿using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;

public class JenkinsBuilder : MonoBehaviour {
	static string[] SCENES = FindEnabledEditorScenes();
	static string APP_NAME = "YourProject";
	static string TARGET_DIR = "build";

	[MenuItem ("Custom/CI/Build iOS Debug")]
	static void PerformiOSDebugBuild ()
	{
		BuildOptions opt = BuildOptions.SymlinkLibraries |
			BuildOptions.Development |
				BuildOptions.ConnectWithProfiler |
				BuildOptions.AllowDebugging |
				BuildOptions.Development;         
		
		PlayerSettings.iOS.sdkVersion = iOSSdkVersion.DeviceSDK; 
		PlayerSettings.iOS.targetOSVersion = iOSTargetOSVersion.iOS_4_3;
		PlayerSettings.statusBarHidden = true;
		
		char sep = Path.DirectorySeparatorChar;
		string buildDirectory = Path.GetFullPath(".") + sep + TARGET_DIR;
		Directory.CreateDirectory(buildDirectory);
		
		string BUILD_TARGET_PATH = buildDirectory + "/ios";
		Directory.CreateDirectory(BUILD_TARGET_PATH);
		
		GenericBuild(SCENES, BUILD_TARGET_PATH, BuildTarget.iPhone, opt);
	}

	[MenuItem ("Custom/CI/Build Android")]
	static void PerformAndroidBuild ()
	{
		string target_filename = APP_NAME + ".apk";
		GenericBuild(SCENES, target_filename, BuildTarget.Android ,BuildOptions.None);
	}
	
	private static string[] FindEnabledEditorScenes() {
		List<string> EditorScenes = new List<string>();
		foreach(EditorBuildSettingsScene scene in EditorBuildSettings.scenes) {
			if (!scene.enabled) continue;
			EditorScenes.Add(scene.path);
		}
		return EditorScenes.ToArray();
	}
	
	static void GenericBuild(string[] scenes, string target_filename, BuildTarget build_target, BuildOptions build_options)
	{
		EditorUserBuildSettings.SwitchActiveBuildTarget(build_target);
		string res = BuildPipeline.BuildPlayer(scenes, target_filename, build_target, build_options);
		if (res.Length > 0) {
			throw new Exception("BuildPlayer failure: " + res);
		}
	}
}
